Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports ClubStarterKit.Data
Imports ClubStarterKit.Web
Imports System.Text.RegularExpressions

Partial Class Poll_AddReaction
    Inherits System.Web.UI.Page

    Private _pollId As Guid

    Public Property PollId() As Guid
        Get
            If _pollId = Guid.Empty Then
                _pollId = DirectCast(ViewState("PollId"), Guid)
            End If
            Return _pollId
        End Get
        Set(ByVal value As Guid)
            ViewState("PollId") = value
        End Set
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            PollId = New Guid(Request("PollId"))
        Catch
            Response.Redirect("~/Poll/List.aspx", True)
        End Try

        If PollId = Guid.Empty Then
            Response.Redirect("~/Poll/List.aspx", True)
        End If

        Dim q As New PollQuestion()
        q = Poll.LoadQuestion(PollId)
        QuestionLabel.Text = q.Question
    End Sub

    Protected Sub UpdateCancelButton_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
        If e.CommandName = "Cancel" Then
            Response.Redirect(e.CommandArgument.ToString(), True)
        End If
    End Sub

    Protected Sub UpdateButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim uGuid As New Guid(Membership.GetUser().ProviderUserKey.ToString())
        Dim reaction As String = HttpUtility.HtmlEncode(ReactionTextBox.Text.Trim())
        'hack to preserve at least some formatting in a post
        reaction = Regex.Replace(reaction, "" & Chr(13) & "" & Chr(10) & "", "<br />", RegexOptions.IgnoreCase Or RegexOptions.Multiline)
        reaction = Regex.Replace(reaction, "&lt;br /&gt;", "<br />", RegexOptions.IgnoreCase Or RegexOptions.Multiline)
        'suggest you get an html editor type input box instead
        'if you do, you'll need to sanitize the input here to check for malicious html and script junk

        ClubStarterKit.Web.Poll.AddPollReaction(reaction, PollId, uGuid)
        Response.Redirect(String.Format("~/Poll/View.aspx?pollId={0}", PollId.ToString()), True)
    End Sub
End Class
